#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTcpSocket>
#include <QHostAddress>
#include <QByteArray>
#include <QTextStream>
#include <QTextCodec>
#include <QDebug>

#include "formserversettings.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //socket = new QTcpSocket(this);
    //client = new NNTPClient(this);

    databaseManager = DatabaseManager::instance();
    connect(databaseManager, SIGNAL(databaseExists(bool)), this, SLOT(databaseExists(bool)));
    databaseManager->start(QThread::LowestPriority);
    databaseManager->checkDatabaseExists();

    connect(ui->actionServers, SIGNAL(triggered()), this, SLOT(showServerSettings()));
}

MainWindow::~MainWindow()
{
    //delete socket;
    //delete client;
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::showServerSettings()
{
    qDebug() << "Opening server settings";
    FormServerSettings *frm = new FormServerSettings;
    frm->show();
}

void MainWindow::databaseExists(bool exists)
{
    if (!exists) {
        databaseManager->createDatabase();
    }
}

